package com.junzhiit.nengji.dao.impl;

import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.bainaeco.framework.base.dao.AbstractBaseDAO;
import com.bainaeco.framework.common.jdbc.StatementParameter;
import com.bainaeco.framework.enums.IsDelete;
import com.junzhiit.nengji.bo.UserVipBO;
import com.junzhiit.nengji.dao.UserVipDAO;
import com.junzhiit.nengji.model.UserVip;
import com.junzhiit.nengji.model.enums.Is;
import com.junzhiit.nengji.model.enums.IsUse;
import com.junzhiit.nengji.model.enums.UserVipType;

@Repository
public class UserVipDAOImpl extends AbstractBaseDAO<UserVip> implements UserVipDAO {

	@Override
	public UserVip checkUser(String userId, String vipId) {
		StringBuffer sql = new StringBuffer();
		sql.append("select * from user_vip where user_id = ? and vip_id = ? and is_delete = ?");
		StatementParameter params= new StatementParameter();
		params.setString(userId);
		params.setString(vipId);
		params.setInt(IsDelete.NO.toIntValue());
		return super.query(sql.toString(),UserVip.class,params);
	}
	
	@Override
	public UserVip checkUserByType(String userId, String vipId) {
		StringBuffer sql = new StringBuffer();
		sql.append("select * from user_vip where user_id = ? and vip_id = ? and is_delete = ? and type = ? ");
		StatementParameter params= new StatementParameter();
		params.setString(userId);
		params.setString(vipId);
		params.setInt(IsDelete.NO.toIntValue());
		params.setInt(Is.YES.toIntValue());
		return super.query(sql.toString(),UserVip.class,params);
	}

	@Override
	public UserVipBO getVipByUserId(String userId) {
		StringBuffer sql = new StringBuffer();
		sql.append("select * from user_vip uv");
		sql.append(" inner join vip v on v.id =uv.vip_id");
		sql.append(" where uv.user_id = ? and is_use = ? and uv.is_delete = ?");
		StatementParameter params = new StatementParameter();
		params.setString(userId);
		params.setInt(IsUse.YES.toIntValue());
		params.setInt(IsDelete.NO.toIntValue());
		return super.query(sql.toString(),UserVipBO.class,params);
	}

	@Override
	public UserVip checkUserVip(String userId) {
		StringBuffer sql = new StringBuffer();
		sql.append("select * from user_vip");
		sql.append(" where is_delete =? and user_id =? and is_use =?");
		StatementParameter params = new StatementParameter();
		params.setInt(IsDelete.NO.toIntValue());
		params.setString(userId);
		params.setInt(IsUse.YES.toIntValue());
		return super.query(sql.toString(),UserVip.class,params);
	}

	@Override
	@Transactional(readOnly = false, propagation = Propagation.REQUIRED)
	public void deleteInvalid() {
		StringBuffer sql = new StringBuffer();
		StatementParameter param = new StatementParameter();
		sql.append(" delete from  user_vip where is_use = ? and type = ? and  timestampdiff(hour,created_time,now()) >=24 ");
		param.setInt(Is.NO.toIntValue());
		param.setInt(UserVipType.BUY.toIntType());
		super.updateForBoolean(sql.toString(), param);
	}

	@Override
	public UserVip checkVipByNumber(String number) {
		StringBuffer sql = new StringBuffer();
		sql.append("select * from user_vip where number =? ");
		StatementParameter param = new StatementParameter();
		param.setString(number);
		return super.query(sql.toString(),UserVip.class,param);
	}
}
